package com.dingqb.mycs.ui.fragment;

import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.base.BaseFragment;
import com.base.datacallback.VolleyCallBackListener;
import com.dingqb.mycs.R;
import com.dingqb.mycs.ui.activity.VolleryTask;
import com.dingqb.mycs.ui.adapter.HistoryTodayListAdapter;
import com.dingqb.mycs.ui.entity.HistoryResult;
import com.google.gson.Gson;

public class MainTab2Fragment extends BaseFragment implements VolleyCallBackListener {

	static final int MENU_SET_MODE = 0;

	private LinkedList<String> mListItems;
	private ListView mPullRefreshListView;
	// 刷新相关
	private int mFrom = 0; // 刷新数据的位置
	private static final int mLength = 20; // 加载的数据长度

	final String key = "d9c0b104852bb610edde357b1ece5327";
	final String v = "1.0";

	String tag = "MainTab2Fragment";
	String url = "http://api.juheapi.com/japi/toh";
	HistoryTodayListAdapter mAdapter;
	ListView actualListView;
	int mDay;
	int mMonth;

	/** Called when the activity is first created. */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_maintab02, container, false);
		initView(view);
		renderPage(0);
		return view;
	}

	private void initView(View view) {
		mPullRefreshListView = (ListView) view.findViewById(R.id.pull_refresh_list);

	}

	@SuppressWarnings("unchecked")
	protected void renderPage(int from) {
		Calendar calendar = Calendar.getInstance();
		mDay = calendar.get(Calendar.DAY_OF_MONTH) + 1;
		mMonth = calendar.get(Calendar.MONTH) + 1;
		HashMap pars = new HashMap();
		pars.put("key", key);
		pars.put("v", v);
		pars.put("month", mMonth + "");
		pars.put("day", mDay + "");
		VolleryTask.getVolleryHistory(MainTab2Fragment.this, tag, url, pars, new Bundle());
	}

	@Override
	public void onResume() {
		super.onResume();
		System.out.println("MainFriendFragment--onResume");
	}

	@Override
	public void onPause() {
		super.onPause();
		System.out.println("MainFriendFragment--onPause");
	}

	@Override
	public void onFailure(Object object, Bundle bundle) {

	}

	@Override
	public void onSuccess(Object object, Bundle bundle) {
		Gson gson = new Gson();
		try {HistoryResult historyResult = gson.fromJson(object.toString(), HistoryResult.class);
		HistoryTodayListAdapter mAdapter = new HistoryTodayListAdapter(getActivity(), historyResult.getResult());
		actualListView.setAdapter(mAdapter);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

}
